<?php
namespace app\home\controller;
use app\common\controller\Homebase;
use app\common\model\Category;
use app\common\model\Model;
use \think\Db;
use \think\Request;

// +----------------------------------------------------------------------
// | XpCms
// +----------------------------------------------------------------------
// | Copyright (c) 2021-2099
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Mercurio（Xp） <725668993@qq.com>
// +----------------------------------------------------------------------

/*
** 前台首页
*/

class Error extends Homebase {
    private static $_options 	= null; // 数据表对象
	
	// 优先加载
	public function  _initialize() {
		parent::_initialize();
		// 实例化数据表模型
		self::$_options = Db::name('Options');
		$request = Request::instance();
		//当前模块
		$this->module = strtolower($request->module());
		//当前控制器
        $this->table = strtolower($request->controller());
        $column = getNID();
        //模型ID
        $this->moduleid = Category::where('typedir','=',$column)
            ->value('mcode');
        //当前表
        $this->tablename = Model::where('id','=',$this->moduleid)
            ->value('tablename');
		
	}
	
	
	//列表页控制器
    public function index()
    {
        
        $column = getNID();
        if($column){
            $cate = Category::where('typedir','=',$column)
                ->find();
            $cate['topid'] = $cate['pid']?$cate['pid']:$cate['id'];
            $cate['url'] = getUrl($cate);
        }
        
        $content = self::$_options -> where(array('title' => 'siteinfo')) -> value('content');
		$site = json_decode($content, true);
            
        if($this->tablename=='page'){
            //单页模型
            //查找第一条记录
            $info = Db::name($this->tablename)
                ->where('cid','=',$cate['id'])
                ->find();
            $this->view->assign('info', $info);//单页内容
            //定义tdk
            $title  = $cate['seo_title'] ? $cate['seo_title'] :
                ($cate['name'] ? $cate['name'] : $info['title']);
            $keywords    = $cate['seo_keys'] ? $cate['seo_keys'] : $site['seo_keys'];
            $description = $cate['seo_desc'] ? $cate['seo_desc'] : $site['seo_desc'];
        }else{
            //列表模型
            //定义tdk
            $title       = $cate['seo_title']       ? $cate['seo_title']       : $cate['name'];
            $keywords    = $cate['seo_keys']    ? $cate['seo_keys']    : $site['seo_keys'];
            $description = $cate['seo_desc'] ? $cate['seo_desc'] : $site['seo_desc'];
            //列表内容
            $where['cid'] = $cate['id'];
            $order = "id desc";
            $p = intval(input('p'))==0?1:intval(input('p'));
            $pagenum = $cate['pagesize'];
            $list  = db($this->tablename)->where($where)->order($order)->limit(($p-1)*$pagenum, $pagenum)->select();
            foreach ($list as &$v) {
                $v['url'] = getShowUrl($v);
            }
            $this->view->assign('list',$list);//列表内容
        }
        
        
        $this->view->assign('cate',        $cate);//栏目信息
        $this->view->assign('site',      $site);//系统信息
        $this->view->assign('public',      './tpl/'.$this->module.'/'.$site['template'].'/');//公共目录
        $this->view->assign('title',       $title);//seo信息
        $this->view->assign('keywords',    $keywords);//seo信息
        $this->view->assign('description', $description);//seo信息

        $template=$cate['listtpl']?str_replace('.html', '', $cate['listtpl']):$this->tablename.'_list';
        return $this->view->fetch($template);
        
    }
    
    //内容页控制器
    public function info(){
        $column = getNID();
        if($column){
            $cate = Category::where('typedir','=',$column)
                ->find();
            $cid = $cate['id'];
            if(input('id')){
                $article = Db::name($this->tablename)->where(array('cid'=> $cid, 'filename'=> input('id')))->find();
                $art = Db::name($this->tablename)->where(array('cid'=> $cid, 'id'=> input('id')))->find();
                if($article){
                    $aid = $article['id'];
                }else if($art){
                    $aid = $art['id'];
                }else{
                    abort(404,'文章不存在');
                }
                $cate['topid'] = $cate['pid']?$cate['pid']:$cate['id'];
                $cate['url'] = getUrl($cate);
                //浏览数增加1
                Db::name($this->tablename)
                    ->where('id',$aid)
                    ->setInc('click');
            }else{
                abort(404,'参数错误');
            }
        }
        $content = self::$_options -> where(array('title' => 'siteinfo')) -> value('content');
        $site = json_decode($content, true);
        //查找详情信息
        $info = Db::name($this->tablename)
            ->where('id',$aid)
            ->find();
        $info = changefield($info,$this->moduleid);
        $info['url'] = getShowUrl($info);

        //定义tdk
        $title  = $info['seo_title'] ? $info['seo_title'] : $info['title'];
        $keywords = $info['seo_keys']?$info['seo_keys']:($cate['seo_keys'] ? $cate['seo_keys'] : $site['seo_keys']);
        $description = $info['seo_desc']?$info['seo_desc']:($cate['seo_desc'] ? $cate['seo_desc'] : $site['seo_desc']);

        $this->view->assign('cate'        , $cate);        //栏目信息
        $this->view->assign('info'        , $info);        //详情信息
        $this->view->assign('site'      , $site);      //系统信息
        $this->view->assign('public'      , './tpl/'.$this->module.'/'.$site['template'].'/');//公共目录
        $this->view->assign('title'       , $title);       //seo信息
        $this->view->assign('keywords'    , $keywords);    //seo信息
        $this->view->assign('description' , $description); //seo信息

        $template=$info['template'] ? $info['template'] : ($cate['contenttpl']?str_replace('.html', '', $cate['contenttpl']):$this->tablename.'_show');
        return $this->view->fetch($template);

    }
	
}
